/*
* Copyright (C) 2008 Google Enterprise EMEA
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.gsa.aasr;

import java.io.IOException;

import java.net.MalformedURLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;


public class AASRServletClickCounter extends HttpServlet {
    private static final String CONTENT_TYPE = 
        "text/html; charset=windows-1252";
    private AASRServiceManager serviceMgr = null;
    private static org.apache.log4j.Logger log4jLogger = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log4jLogger = Logger.getLogger(AASRServletClickCounter.class);
        serviceMgr = new AASRServiceManager();
        log4jLogger.debug("Service Manager instantiated");
        try {
            serviceMgr.loadServices();
            log4jLogger.debug("All Services loaded by Service Manager");
        } catch (ConfigurationException e) {
            log4jLogger.error(e);
        } catch (MalformedURLException e) {
            log4jLogger.error(e);
        } catch (ClassNotFoundException e) {
            log4jLogger.error(e);
        } catch (IllegalAccessException e) {
            log4jLogger.error(e);
        } catch (InstantiationException e) {
            log4jLogger.error(e);
        }
    }


    /**Process the HTTP doGet request.
     */
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, 
                                                           IOException {
        String attNames = (request.getAttributeNames()).toString();
        AASRService service = null;

        String queryString = request.getQueryString();
        log4jLogger.debug("QueryString: " + queryString);

        String params[] = queryString.split("&");

        for (int i = 0; i < params.length; i++) {
            String values[] = params[i].split("=");
            if (values.length == 2)
                if (values[0].equals("fend")) {
                    log4jLogger.debug("Frontend logging:" + values[1]);
                    service = serviceMgr.getService(values[1]);
                    log4jLogger.debug("Service instantiated");
                }
        }

        //try {

        try{
            service.logClick(params);
            log4jLogger.debug("Click logged");
        } catch (AASRLogException e){
            log4jLogger.debug(e.getMessage());
        }
    }

}
